/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.zprofilefrontend.model;

import com.vng.zing.calc.thrift.Profile;
import com.vng.zing.calc.thrift.Profile;
import com.vng.zing.calc.thrift.ProfileSession;
import com.vng.zing.logger.ZLogger;
import com.vng.zing.stats.Profiler;
import com.vng.zing.stats.ThreadProfiler;
import com.vng.zing.zcommon.thrift.PutPolicy;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author namhh
 */
public class FacebookLoginModel extends BaseModel {

    private static final Logger _Logger = ZLogger.getLogger(FacebookLoginModel.class);
    public static final FacebookLoginModel Instance = new FacebookLoginModel();

    @Override
    public void process(HttpServletRequest req, HttpServletResponse resp) {
        ThreadProfiler profiler = Profiler.getThreadProfiler();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = null;
        try {
            out = resp.getWriter();
            ///TODO..
            profiler.push(this.getClass(), "output");
            if (null != req.getParameter("code")) {
                String code = req.getParameter("code");
                Map<String, Object> result = FacebookApi.getInstance().getProfileFrom(code);
                Profile FBProfile = (Profile) result.get("profile");
                Profile isExist = getClient().getSocialProfile(FBProfile.getSource(), FBProfile.getUID());
                String accessToken = (String) result.get("token");
                Integer expAccessTokenTime = (Integer) result.get("exp");
                ProfileSession profileSession = null;
                if (null == isExist) {
                    Profile profile = getClient().createProfile(FBProfile);
                    if (null != profile) {
                        getClientWithCache().putCache(profile.getId(), profile, PutPolicy.ADD);
                        profileSession = new ProfileSession(profile.getId(), accessToken, expAccessTokenTime);
                        setLoggedIn(req, resp, profileSession, false);
                        resp.sendRedirect("/info?id=" + profile.getId());
                    } else {
                        resp.sendRedirect("/login");
                    }
                } else {

                    getClientWithCache().putCache(isExist.getId(), isExist, PutPolicy.ADD_OR_UDP);
                    profileSession = new ProfileSession(isExist.getId(), accessToken, expAccessTokenTime);
                    setLoggedIn(req, resp, profileSession, false);
                    resp.sendRedirect("/info?id=" + isExist.getId());
                }
            }
            profiler.pop(this.getClass(), "output");
        } catch (Exception ex) {
            _Logger.error(null, ex);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

}
